#ifndef TCPCLIENT_H
#define TCPCLIENT_H

#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QTcpSocket>
#include <QVector>
#include <utility>

using std::string;
using json = QJsonObject;
using jsonDoc = QJsonDocument;
using jsonArray = QJsonArray;
using Qt::endl;

enum class EnMsgType {
    LOGIN_MSG = 1,  // 登录消息
    LOGIN_OUT_MSG,  // 注销消息
    REG_MSG,        // 注册消息
    LOGIN_MSG_ACK,  // 登录响应消息
    REG_MSG_ACK,    // 注册响应消息
    ONE_CHAT_MSG,   // 单一聊天消息
    ADD_FRIEND_MSG, // 添加好友消息

    CREATE_GROUP_MSG, // 创建聊天组
    ADD_GROUP_MSG,    // 加入聊天组
    GROUP_CHAT_MSG,   // 组聊天
};

enum ContentType { TextOnly, ImageOnly, MixedContent };

typedef struct self_info {
    int account = 0;
    QString password;
    QString name;
} SelfInfo;

typedef struct friend_info {
    int account = 0;
    QString name;
    bool isOnline = false;
    friend_info(int _account, QString _name) {
        name = std::move(_name);
        account = _account;
    }
    friend_info() = default;
} FriendInfo;

typedef struct member_info {
    int account = 0;
    QString name;
} MemberInfo;

typedef struct group_info {
    int groupAccount;
    QString groupName;
    QVector<MemberInfo> memberList;
} GroupInfo;

class TcpClient : public QTcpSocket {
    Q_OBJECT
public:
    explicit TcpClient(QObject *parent = nullptr);
    ~TcpClient() override;
    int ConnectToServer();
    void DisconnectFromServer();

public:
    void SendMsg(json message);
    void CmdParser(json message);

Q_SIGNALS:
    // 收到消息信号
    void CallClient(json msg);
    void CallLogging(json msg);
    void CallAddFriend(json msg);

public slots:
    void onReadyRead();

public:
    QString m_server = "192.168.6.2";

    int m_port = 6000;
    bool m_isConnected = false;

    json message;

private:
    QByteArray buffer;
};
#endif // TCPCLIENT_H
